package com.crazy.web.controller.user;

import cn.hutool.core.exceptions.ValidateException;
import com.crazy.enums.Constants;
import com.crazy.model.major.entity.TUserLimit;
import com.crazy.rest.RestResult;
import com.crazy.web.controller.BaseController;
import com.crazy.web.service.user.AuthorityService;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhaochaofeng
 * @version 1.0
 * @date 2020/12/15 11:39
 */
@Controller
@Log4j2
@RequestMapping("/user/authority")
public class AuthorityController extends BaseController {

    @Autowired
    private AuthorityService authorityService;

    @RequiresPermissions("authority:view")
    @GetMapping("/view")
    public String view() {
        request.setAttribute("url", "/user/authority/view");
        return "authority/view";
    }

    @RequiresPermissions("authority:list")
    @PostMapping("/list")
    @ResponseBody
    public RestResult list() {
        return RestResult.createSuccessResult("查询成功", authorityService.list());
    }

    @RequiresPermissions("authority:delete")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult delete(@RequestParam Long id) {
        authorityService.deleteById(id);
        return RestResult.createSuccessResult("删除成功");
    }

    @RequiresPermissions("authority:add")
    @PostMapping("/add")
    @ResponseBody
    public RestResult add(@RequestBody TUserLimit tUserLimit) {
        authorityService.saveOrUpdate(tUserLimit);
        return RestResult.createSuccessResult("添加成功");
    }

    @RequiresPermissions("authority:detail")
    @PostMapping("/detail")
    @ResponseBody
    public RestResult detail(@RequestParam Long id) {
        TUserLimit tUserLimit = authorityService.detail(id);
        return RestResult.createSuccessResult("添加成功", tUserLimit);
    }
}
